package uia.alumni.web;

import java.io.PrintWriter;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;

import uia.alumni.data.*;
import uia.alumni.group.ListGroups;
import uia.alumni.pms.*;
import uia.alumni.profile.*;
import uia.alumni.search.Search;
import uia.alumni.group.*;

import static uia.alumni.form.FieldType.*;
import static uia.alumni.web.Constants.*;
import static uia.alumni.profile.Constants.*;
import static uia.alumni.group.Constants.*;
/**
 * This servlet outputs the application's start page.
 * @author Even Åby Larsen (even.larsen@uia.no)
 * @author Simon Zimmermann
 */
public class Home extends Page {
    public static final String SERVLET_NAME = "home";

    @PersistenceUnit(name=PERSISTENCE_UNIT)
    private EntityManagerFactory emf;


    public Home() {
        super(Role.member);
    }
    
    public void writeBody(PrintWriter out, HttpServletRequest request) {
        out.println("<h1>IS202-Group9's Alumni</h1>");
        out.println("<p>This file is currently used as a sitemap sort of thing.</p>");
        out.println("<h2>Users:</h2>");

        EntityManager em = emf.createEntityManager();
        Query q = em.createNamedQuery("User.findAll");
        List<User> users = (List<User>)q.getResultList();
        out.println("<ul>");
        for(User user : users){
            out.println(tagLi(tagA(ViewProfile.SERVLET_NAME, user.getFullName(), UID, Long.toString(user.getUid()))));
        }
        out.println("</ul>");
        out.println("<h2>Groups:</h2>");
        q = em.createNamedQuery("Group.findAll");
        List<Group> groups = (List<Group>)q.getResultList();
        out.println("<ul>");
        for(Group group : groups){
            out.println(tagLi(tagA(ViewGroup.SERVLET_NAME, group.getName(), GID, Long.toString(group.getId()))));
        }
         out.println("</ul>");
        out.println("<h2>Servlets:</h2>");
        out.println("<ul>");
            out.println(tagLi(tagA(RegisterProfile.SERVLET_NAME, "Register a Profile")));
            out.println(tagLi(tagA(Pms.SERVLET_NAME, "PMS inbox")));
            out.println(tagLi(tagA(ReadPms.SERVLET_NAME, "ReadPms")));
            out.println(tagLi(tagA(WritePms.SERVLET_NAME, "Write Pms")));
            out.println(tagLi(tagA(Search.SERVLET_NAME, "Search")));
            out.println(tagLi(tagA(ListGroups.SERVLET_NAME, "List groups")));
        out.println("</ul>");
    }
    public final static long serialVersionUID = 1;
}
